﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoforGit
{
    class Game
    {
        /// <summary>
        /// 游戏得分
        /// </summary>
        public int GameScore { get; set; }

        private const int GameWidth = 1000;
        private const int GameHeight = 700;
        private List<Blocks> Blocks { get; set; }
        /// <summary>
        /// 游戏自带的Timer
        /// </summary>
        private System.Timers.Timer Timer { get; set; }

        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;

  

        private Point[] blockPoints =
        {
           
           new Point(800,200),
        };
       
        private Chess _chess;
        private Blocks _blocks;
        private Blocks __blocks;
        private Blocks ___blocks;

        public Game()
        {           
            _chess = new Chess(new Rectangle(200 - 25, 250 - 100, 50, 100));
            //初始化Blocks
            Blocks = new List<Blocks>();
            //初始化分数
            GameScore = 0;
           
                        
            foreach (var blockPoint in blockPoints)
            {
                _blocks = new Blocks(new Rectangle(100,300,200,150));
                Blocks.Add(_blocks);              
            }
            foreach (var _blockPoint in blockPoints)
            {
                __blocks = new Blocks(new Rectangle(400,150,200,150));
                Blocks.Add(__blocks);
            }
            foreach (var __blockPoint in blockPoints)
            {
                ___blocks = new Blocks(new Rectangle( __blockPoint, new Size(200, 150)));
                Blocks.Add(___blocks);
            }

            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
            



        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟处理程序
            _blocks.Move();
            __blocks.Move();
            ___blocks.Move();
            _chess.Move(new Size(GameWidth,GameHeight));
            //处理碰撞
            HandleTouch();
            //通知form游戏改变了
            gameChanged();
        }
        /// <summary>
        /// 处理碰撞
        /// </summary>
        private void HandleTouch()
        {

            Blocks blockNow = null;
            foreach (var block in Blocks)
            {
                if (_chess.Rectangle.IntersectsWith(block.Rectangle))
                {
                    //棋子碰到方块 
                    _chess.ChangeVerDiction();
                    blockNow = block;//记下来
                    //棋子每碰到一次方块，得五分
                    
                    break;
                }
 
            }
            if (blockNow!=null)
            {
                //棋子每碰到一次方块，得五分
                GameScore += 5;
            }

          



        }

        /// <summary>
        /// 绘图游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g,Size size)
        {
            //定义一个空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));

            _g.DrawImage(Properties.Resources.bg, rec);
           
            _chess.Draw(_g);

            //分数
            Rectangle rectangle = new Rectangle(0, 0, 90,120 );
            _g.DrawImage(Properties.Resources.分数, rectangle);
            //画数字
            Font drawFont = new Font("Arial", 32);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            _g.DrawString(GameScore.ToString(), drawFont, drawBrush, 30, 50);
      
            foreach (var block in Blocks)
            {
                block.Draw(_g);
            }






            //todo
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            _chess.KeyDown(key);
        }

        /// <summary>
        /// 判断游戏是否结束
        /// </summary>
        /// <returns>游戏是否结束，true为结束</returns>
       public bool GameOver()
        {
            if (this._chess.Rectangle.Y +this._chess.Rectangle.Height>GameHeight)
            {
                Timer.Stop();
                return true;
            }
            return false;

        }

 
    }
}
